package Controller;


import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import Jdbc.Jdbc;
import Model.Rol;
import Model.SistemYoneticisi;
import Model.Uye;

import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport{
	
	private String e_mail;
	private String sifre;
	private int giris_tipi;
	private InputStream inputStream;
	private String sonuc;
	private Uye uye;
	private SistemYoneticisi admin;
	private int cikis_tipi;
	
	public String login() throws UnsupportedEncodingException{
		
		if(giris_tipi==0){
			uye=Jdbc.uyeSorgula(e_mail,sifre);
			if(uye!=null){
				sonuc = "basarili_giris";
				setSession("uye", uye);
				
			}
			else{
				sonuc="error";
			}
		}
		else{
			if(sifre.equals("123")&&e_mail.equals("admin")){
				admin=new SistemYoneticisi();
				sonuc="admin_girisi";
				setSession("admin", admin);
			}
			else{
				sonuc="error";
			}
		}
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
		
	}

	public String cikisYap() throws UnsupportedEncodingException{
		
		sonuc="basarili_cikis";
		if(cikis_tipi==0){
			setSession("uye", null);
		}
		else{
			setSession("admin", null);
		}
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
		
	}
	
	
	
	public int getCikis_tipi() {
		return cikis_tipi;
	}

	public void setCikis_tipi(int cikis_tipi) {
		this.cikis_tipi = cikis_tipi;
	}

	public String getE_mail() {
		return e_mail;
	}


	public void setE_mail(String e_mail) {
		this.e_mail = e_mail;
	}


	public String getSifre() {
		return sifre;
	}


	public void setSifre(String sifre) {
		this.sifre = sifre;
	}


	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}


	public int getGiris_tipi() {
		return giris_tipi;
	}


	public void setGiris_tipi(int giris_tipi) {
		this.giris_tipi = giris_tipi;
	}
	
	private static void setSession(String key, Object obj) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setAttribute(key, obj);
	}
	
}
